﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Facebook.Web;
using WhsFacebook.Whs;

namespace WhsFacebook.Net.Web.Facebook
{
    public partial class MyWhs_v2 : CanvasFBMLBasePage
    {
        public MyWhs_v2()
        {
            this.RequireLogin = true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            using (var db = new Database.WhsFacebookEntities())
            {
                // query instance id
                var defMaster = (DefaultMaster)this.Master;
                var userId = defMaster.FacebookVisit.UserId;

                var instanceId = (
                    from t in db.FacebookInstanceMappings
                    where t.FacebookUserId == userId
                    select t.InstanceId
                    ).First();

                this.whsInstanceId.InnerText = instanceId.ToString();

                // query system info
                {
                    var i = db.UserSystemInfosLatests.Where(o => o.InstanceId == instanceId).FirstOrDefault();

                    if (i == null)
                    {
                        WhsNameValueListHelpers.AddDtDd(this.systemInfo.Controls, "ERROR", "No data has yet been submitted. Please make sure the add-ins is running on your WHS by opening the 'Facebook' tab in the WHS Console settings tab.");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(i.SystemManufacturer))
                            WhsNameValueListHelpers.AddDtDd(this.systemInfo.Controls, "Manufacturer", i.SystemManufacturer);

                        if (!string.IsNullOrEmpty(i.SystemManufacturer))
                            WhsNameValueListHelpers.AddDtDd(this.systemInfo.Controls, "Model", i.SystemModel);

                        if (i.TotalRam.HasValue && (i.TotalRam.Value > 0))
                            WhsNameValueListHelpers.AddDtDd(this.systemInfo.Controls, "RAM", WhsFacebookHelpers.GetNiceRamSize(i.TotalRam.Value));

                        if (!string.IsNullOrEmpty(i.WhsVersion))
                            WhsNameValueListHelpers.AddDtDd(this.systemInfo.Controls, "Home Server Version", i.WhsVersion);

                        if (!string.IsNullOrEmpty(i.OsVersion))
                            WhsNameValueListHelpers.AddDtDd(this.systemInfo.Controls, "Windows Version", i.OsVersion);

                        if (!string.IsNullOrEmpty(i.WhsDomain))
                        {
                            WhsNameValueListHelpers.AddDtDdHtml(
                                this.systemInfo.Controls, 
                                "Web Access",
                                string.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", i.WhsDomain)
                                );
                        }
                    }
                }

                // query tower image
                {
                    var img = db.UserWhsTowerImages.Where(o => o.InstanceId == instanceId).FirstOrDefault();

                    if (img == null)
                    {
                        this.towerImage.Src = this.ResolveUrlAbsolute("~/Images/NoTowerImage.jpg");
                    }
                    else
                    {
                        this.towerImage.Src = string.Format(
                            "{0}?id={1}",
                            this.ResolveUrlAbsolute("~/Facebook/Handlers/TowerImage.ashx"),
                            instanceId
                            );
                    }
                }

                // storage data
                {
                    // link image
                    this.storageChart.Src = string.Format(
                        "{0}?id={1}",
                        this.ResolveUrlAbsolute("~/Facebook/Handlers/StorageChartJpeg.aspx"),
                        instanceId
                        );

                    // storage details
                    var i = db.UserStorageInfosLatests.Where(o => o.InstanceId == instanceId).FirstOrDefault();

                    if (i == null)
                    {
                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "ERROR", "No data has yet been submitted. Please make sure the add-ins is running on your WHS by opening the 'Facebook' tab in the WHS Console settings tab.");
                    }
                    else
                    {
                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "Storage", string.Format(
                            "{0} used, {1} free, {2} total",
                            WhsFacebookHelpers.GetNiceFileSize(i.StorageUsedSpace),
                            WhsFacebookHelpers.GetNiceFileSize(i.StorageFreeSpace),
                            WhsFacebookHelpers.GetNiceFileSize(i.StorageSize)
                            ));

                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "System", string.Format(
                            "{0} operating system, {1} app folders",
                            WhsFacebookHelpers.GetNiceFileSize(i.OperatingSystemSize),
                            WhsFacebookHelpers.GetNiceFileSize(i.AppFoldersSize)
                            ));

                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "Backups", string.Format(
                            "{0} client computer backups",
                            WhsFacebookHelpers.GetNiceFileSize(i.BackupSize)
                            ));

                        WhsNameValueListHelpers.AddDtDd(this.storageDetails.Controls, "Files", string.Format(
                            "{0} shared folders, {1} duplicates",
                            WhsFacebookHelpers.GetNiceFileSize(i.SharedFoldersSize),
                            WhsFacebookHelpers.GetNiceFileSize(i.SharedFoldersDupSize)
                            ));
                    }
                }

                // query addins
                {
                    var i = db.UserInstalledAddinsLatestExes.Where(o => o.InstanceId == instanceId).OrderBy(o => o.Title).ToArray();

                    if (i.Length <= 0)
                    {
                        var li = new HtmlGenericControl("li");

                        li.InnerText = "No addins installed on your WHS (or no data yet reported).";

                        this.addinList.Controls.Add(li);
                    }
                    else
                    {
                        foreach (var a in i)
                        {
                            var li = new HtmlGenericControl("li");

                            li.InnerText = string.Format(
                                "{0} ({1}, {2})",
                                a.Title,
                                a.Version,
                                a.Author
                                );

                            this.addinList.Controls.Add(li);
                        }
                    }
                }

                // query disks
                {
                    var i = db.UserDiskInfosLatests.Where(o => o.InstanceId == instanceId).OrderByDescending(o => o.Capacity).ToArray();
                    
                    if (i.Length <= 0)
                    {
                        var li = new HtmlGenericControl("li");

                        li.InnerText = "No data yet reported.";

                        this.diskList.Controls.Add(li);
                    }
                    else
                    {
                        foreach (var a in i)
                        {
                            var li = new HtmlGenericControl("li");

                            li.InnerText = string.Format(
                                "{0}, {1} ({2}, {3})",
                                a.DiskModelNice,
                                WhsFacebookHelpers.GetNiceFileSize(a.Capacity),
                                a.BusType,
                                a.DiskRole
                                );

                            this.diskList.Controls.Add(li);
                        }
                    }
                }
            }

        }
    }
}